package com.object;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Item {
    //商品类  ID  名称  生产商   价格  库存  类别
    private String id;
    private String name;
    private  String factory;
    double price;
    int stock =10;
    String type;

    public Item() {
    }

    public Item(String id, String name, String factory, double price, String typeh) {
        this.id = id;
        this.name = name;
        this.factory = factory;
        this.price = price;
        this.type = type;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFactory() {
        return factory;
    }

    public void setFactory(String factory) {
        this.factory = factory;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
    public  ArrayList<Item> initData(User user,ArrayList<Item> items){
        Item item1 = new Item(user.newUid(4),"磨牙棒","汪汪队",15,"玩具");
        Item item2 = new Item(user.newUid(4),"惨叫鸡","汪汪队",12,"玩具");
        Item item3 = new Item(user.newUid(4),"狗粮","汪汪队",200,"零食");
        Item item4 = new Item(user.newUid(4),"拾便器","汪汪队",10,"清洁");
        Item item5 = new Item(user.newUid(4),"感冒药","汪汪队",30,"医疗");
        Item item6 = new Item(user.newUid(4),"牛排","汪汪队",30,"零食");
        Item item7 = new Item(user.newUid(4),"除臭剂","汪汪队",25,"清洁");
        Item item8 = new Item(user.newUid(4),"退烧药","汪汪队",50,"医疗");
        items.add(item1);
        items.add(item2);
        items.add(item3);
        items.add(item4);
        items.add(item5);
        items.add(item6);
        items.add(item7);
        items.add(item8);
        return items;
    }
    public String chooseType(Scanner scanner){
        System.out.println("请选择分类查看 1:全部   2:玩具   3:零食   4:清洁   5:医疗");
        return scanner.next();
    }

    //展示所有商品
    public void showAll(ArrayList<Item> items){
        System.out.println("id     名称     生产商     价格    库存    类别");
        for (Item item : items) {
            System.out.println(item.getId()+"    "+item.getName()+"    "+item.getFactory()+"    " +
                    item.getPrice()+"     "+item.getStock()+"     "+item.getType());
        }
    }
    //展示指定分类的商品
    public void showType(ArrayList<Item> items,String type){
        System.out.println("id     名称     生产商     价格    库存    ");
        for (Item item : items) {
            if(item.getType().equals(type)){
                System.out.println(item.getId()+"    "+item.getName()+"    "+item.getFactory()+"    " +
                        item.getPrice()+"     "+item.getStock());
            }
        }
    }
    public void showItems(String type,ArrayList<Item> items){
        switch (type){
            case "1":
                showAll(items);
                break;
            case "2":
                showType(items,"玩具");
                break;
            case "3":
                showType(items,"零食");
                break;
            case "4":
                showType(items,"清洁");
                break;
            case "5":
                showType(items,"医疗");
                break;
        }
    }
    //选择数量
    public void chooseNum(ArrayList<Item>items,Scanner scanner,String id,User user){
        System.out.println("请输入要购买的数量");
        int num = scanner.nextInt();
        Item item1 = new Item();
        for (Item item : items) {
            if(item.getId().equals(id)){
                item1 =item;
                break;//先获取这个商品
            }
        }
        for (Item itemCart : user.getItemCarts()) {
            if(itemCart.getId().equals(id)){
                if((num+itemCart.getStock())>item1.getStock()){
                    System.out.println("您的购物车里已经有"+itemCart.getStock());
                    chooseNum(items,scanner,id,user);
                    break;
                }else{
                    //反之在原有数量上增加
                    itemCart.setStock(itemCart.getStock()+num);
                    System.out.println("已添加至购物车");
                }
            }
        }
    }
    public void chooseNum(User user,String id,ArrayList<Item>items,Scanner scanner){
        System.out.println("请选择要购买的数量");
        int num = scanner.nextInt();
        for (Item item : items) {
            if(id.equals(item.getId())){
                if (num>item.getStock()){
                    System.out.println("库存不足，请重新输入");
                    chooseNum(user,id,items,scanner);
                }else {
                    //否则添加进购物车
                    Item item1 = new Item();
                    item1.setId(item.getId());
                    item1.setFactory(item.getFactory());
                    item1.setName(item.getName());
                    item1.setType(item.getType());
                    item1.setName(item.getName());
                    item1.setPrice(item.getPrice());
                    item1.setStock(num);
                    user.getItemCarts().add(item1);
                    System.out.println("已添加至购物车");
                }
            }
        }
    }
    //判断购买的商品在不在购物车内
    public  boolean idInCarts(User user,String id){
        for (Item itemCart : user.getItemCarts()) {
            if(id.equals(itemCart.getId())){
                return true;
            }
        }
        return false;
    }
    public User buy(ArrayList<Item>items,User user,Scanner scanner){
        System.out.println("请选择您要添加的编号----->输入1: 返回");
        String id = scanner.next();
        if(id.equals("1")){
            return user;
        }
        if(enterId(items,id)){//确认id在不在
            //再判断id在不在购物车内
            if(idInCarts(user,id)){
                chooseNum(items,scanner,id,user);
            }else{
                chooseNum(user,id,items,scanner);
            }

        }else{
            System.out.println("id不存在，重新输入");
            buy(items,user,scanner);
        }
        return user;
    }
    public  boolean enterId(ArrayList<Item>items,String id){
        for (Item item : items) {
            if(id.equals(item.getId())){
                return true;
            }
        }
        return false;
    }
    public boolean conAdd(Scanner scanner){
        System.out.println("是否继续？  1:继续    其他:不了");
        return scanner.next().equals("1");
    }
}
